package com.example.common.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.common.utils.CustomToastUtil;

/**
 * @author skygge
 * @date 2020/7/30.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：Fragment基类
 */
public class BaseFragment extends Fragment {
    protected String TAG = getClass().getSimpleName();
    protected Activity mContext;

    private BaseLoadingDialog dialog;
    private boolean mIsPrepare = false;		//视图还没准备好
    private boolean mIsVisible= false;		//不可见
    private boolean mIsFirstLoad = true;	//第一次加载

    protected Handler mHandler;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        mHandler = new Handler();
        initialize();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mIsPrepare = true;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
        initDataBus();
        initListener();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            mIsVisible = true;
            lazyLoad();
            onUserVisible();
        } else {
            mIsVisible = false;
        }
    }

    protected int getLayoutId(){
        return -1;
    }

    protected void initialize() {
    }

    /**
     * 初始化控件
     */
    protected void initView() {
    }

    /**
     * 初始化数据
     */
    protected void initData() {

    }

    /**
     * 消息事件
     */
    protected void initDataBus(){

    }

    /**
     * 监听事件写在这
     */
    protected void initListener() {
    }

    /**
     * 实现懒加载
     */
    private void lazyLoad() {
        //这里进行三个条件的判断，如果有一个不满足，都将不进行加载
        if (!mIsPrepare || !mIsVisible||!mIsFirstLoad) {
            return;
        }
        onFirstUserVisible();
        //数据加载完毕,恢复标记,防止重复加载
        mIsFirstLoad = false;
    }

    /**
     * 第一次fragment可见（进行初始化工作）
     */
    protected void onFirstUserVisible() {

    }

    /**
     * fragment可见（切换回来或者onResume, 用于刷新数据）
     */
    protected void onUserVisible() {

    }

    protected void skipAnotherActivity(Class<?> mClass){
        startActivity(new Intent(mContext, mClass));
    }

    protected void skipAnotherActivity(Bundle bundle, Class<?> clazz) {
        Intent intent = new Intent(mContext, clazz);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    protected void showToast(String content) {
        CustomToastUtil.showToast(mContext, content);
    }

    protected void showToast(int strRes) {
        CustomToastUtil.showToast(mContext, strRes);
    }

    protected void showProgressDialog() {
        if (dialog == null){
            dialog = new BaseLoadingDialog(mContext);
            dialog.show();
            mHandler.postDelayed(mRunnable, 5000);
        }
    }

    protected void dismissProgressDialog() {
        mHandler.post(mRunnable);
    }

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (dialog!=null){
                dialog.dismiss();
                dialog = null;
            }
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mIsFirstLoad=true;
        mIsPrepare=false;
        mIsVisible = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacks(mRunnable);
        mHandler.removeCallbacksAndMessages(null);
    }
}
